<?php
namespace app\techn_api_n\controller\v1;
use think\Db;
class Bindbank extends Base
{
	public function _initialize()
	{
		parent::_initialize();
		$this->IsLogin(); //验证登录
		$this->IsTechn(); //验证技师
	}

	/**
	 * 获取技师提现账号信息
	 */
	public function detail()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['techn'] = $this->techn;
		//dump($params);
		$map = [
        	'identity_type' => 'techn',
        	'identity_id' => $params['techn']['id']
        ];
        $info = Db::name('bind_bank')->where($map)->find();
        $result_data = [];
        if(!empty($info))
        {
        	$result_data = [
        		'id' => $info['id'],
        		'bank_username' => $info['bank_username'],
        		'bank_accounts' => $info['bank_accounts'],
        	];
        }else
        {
        	$result_data = [
        		'id' => '',
        		'bank_username' => $params['techn']['real_name'],
        		'bank_accounts' => '',
        	];
        }
        $result['code'] = 0;
	    $result['msg'] = "请求成功";
	    $result['data'] = $result_data;
	    echo json_encode($result);
	}

	/**
	 * 保存技师收款账号信息
	 */
	public function save()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		$params = $this->data_post;
		$params['techn'] = $this->techn;
		//dump($params);
		//表单验证
		$validate = $this->validate(
			$params,
			[
				'bank_username' => 'require',
				'bank_accounts' => 'require'
			],
			[
				'bank_username.require' => '请填写姓名',
				'bank_accounts.require' => '请填写账号'
			]
		);
		if(true !== $validate)
        {
            echo json_encode(['code' => -1, 'msg' => $validate]);
            exit();
        }
        //基础数据
        $data = [
        	'identity_type' => 'techn',
        	'identity_id' => $params['techn']['id'],
        	'bank_name' => '支付宝',
        	'bank_accounts' => $params['bank_accounts'],
        	'bank_username' => $params['bank_username']
        ];
        // 添加/编辑
        if(empty($params['id']))
        {
        	$data['add_time'] = time();
        	$data['upd_time'] = time();
        	$bind_bank_id = Db::name('bind_bank')->insertGetId($data);
        }else
        {
        	$data['upd_time'] = time();
        	if(Db::name('bind_bank')->where(['id'=>intval($params['id'])])->update($data))
        	{
        		$bind_bank_id = $params['id'];
        	}
        }
        // 是否成功
        if(isset($bind_bank_id) && $bind_bank_id > 0)
        {
        	$result['code'] = 0;
			$result['msg'] = "操作成功";
        }else
        {
        	$result['code'] = -1;
			$result['msg'] = "操作失败";
        }
        echo json_encode($result);
	}
}